package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public PageResult<Brand> page(Integer currentPage, Integer pageSize, String sortBy, Boolean desc, String search) {
        //开始分页(这一行下面的代码开始分页,需要分页的代码中间最好不要间隔其他代码)
        PageResult<Brand> pageResult = new PageResult<>();
        PageHelper.startPage(currentPage, pageSize);
        //动态sql(涉及到条件的判断的时候使用example对象)  select * from xxx order by id desc
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + (desc ? " desc" : " asc"));//这个方法出现相当远写好了order by
        }
        //用户输入关键字查询
        if (StringUtils.isNotBlank(search)) {
            Example.Criteria criteria = example.createCriteria();
            criteria.andLike("name", "%" + search + "%").orEqualTo("letter", search.toUpperCase());
            //相当于,where letter like % search %
            //search.toUpperCase()  这个方法的作用是全部将字母转化为大写
        }
        //开始查询
        List<Brand> select = brandMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(select);
        //总条数
        long total = pageInfo.getTotal();
        //总页数
        int pages = pageInfo.getPages();
        //结果集
        List list = pageInfo.getList();
        //封装结果
        pageResult.setTotal(total);
        pageResult.setTotalPage((long) pages);
        pageResult.setItems(list);
        //返回
        return pageResult;
    }

    /**
     * 品牌的新增
     * 这里的中间表并没有与两张多的一方的的主键作为自己的外键.涉及到两张表的新增需要考虑事务
     *
     * @param brand
     * @param cids
     */
    @Transactional
    @Override
    public void addBrand(Brand brand, List<Long> cids) {
        //添加品牌表(通用mapper负责插入数据)
        this.brandMapper.insertSelective(brand);
        //通过代码关系维持中间表的关系
        cids.forEach(cid -> {
            this.brandMapper.addCategoryAndBrand(cid, brand.getId());
        });

    }

    /**
     * 品牌的修改,涉及到品牌和分类的中间表
     *
     * @param brand
     * @param cids
     */
    @Override
    public void updateBrand(Brand brand, List<Long> cids) {
        //修改后品牌表的新增
        this.brandMapper.updateByPrimaryKeySelective(brand);
        //对于中间表可以先删除品牌对应的所有分类
        this.brandMapper.deleteCategory(brand.getId());
        cids.forEach(cid -> {
            this.brandMapper.addCategoryAndBrand(cid, brand.getId());
        });
    }

    /**
     * 商品的列表的名称的查询,需要展示商品的品牌名称信息
     *
     * @param brandId
     */
    @Override
    public Brand queryBrandById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        return brand;
    }

    /**
     * 商品新增页面的品牌名称查询(一个分类有多个品牌)
     *
     * @param cid
     * @return
     */
    @Override
    public List<Brand> queryBrandByCategory(Long cid) {
        List<Brand> list = brandMapper.queryBrandByCategory(cid);
        return list;
    }

    @Override
    public Brand queryBrandByBid(Long brandId) {
        Brand brand = this.brandMapper.selectByPrimaryKey(brandId);
        return brand;
    }
}
